<template>
  <div class="pagination">
    <button @click="$emit('getPage',pageNo-1)" :disabled="pageNo==1">上一页</button>
    <button v-if="startAndEnd.start>1" @click="$emit('getPage',1)">1</button>
    <button v-if="startAndEnd.start>2" >···</button>
<!-- -------------------------------- -->
    <button v-for="(val,index) in startAndEnd.end" :key="index" v-show="val>=startAndEnd.start" @click="$emit('getPage',val)">{{val}}</button>
<!-- -------------------------------- -->
    <button v-if="startAndEnd.end<pageTotal-1">···</button>
    <button v-if="startAndEnd.end<pageTotal">{{pageTotal}}</button>
    <button @click="$emit('getPage',pageNo+1)" :disabled="pageNo==pageTotal">下一页</button>
    
    <button style="margin-left: 30px">共 {{total}} 条</button>
    <div>{{startAndEnd.start+"和"+startAndEnd.end}}</div>
  </div>
</template>

<script>
import { computed } from 'vue'
  export default {
    name: "Pagination",
    props:['pageNo','pageSize','total','continue'],
    computed:{
        // 计算总共的页数
        pageTotal(){
            return Math.ceil(this.total/this.pageSize)
        },
        // 计算start和end的值
        startAndEnd(){
            let start=0
            let end=0
            // 第一种情况：总有页数<continue
            if(this.pageTotal<this.continue){
                start=1
                end=this.pageTotal
            }else{
                // 情况二:start为负数了
                start=this.pageNo-parseInt(this.continue/2)
                end=this.pageNo+parseInt(this.continue/2)
                if(start<=0){
                    start=1
                    end=this.continue
                }
                if(end>this.pageTotal){
                    end=this.pageTotal
                    start=this.pageTotal-this.continue+1
                }
            }
            return {start,end}
        }
    }
  }
</script>

<style lang="less" scoped>
  .pagination {
    text-align: center;
    button {
      margin: 0 5px;
      background-color: #f4f4f5;
      color: #606266;
      outline: none;
      border-radius: 2px;
      padding: 0 4px;
      vertical-align: top;
      display: inline-block;
      font-size: 13px;
      min-width: 35.5px;
      height: 28px;
      line-height: 28px;
      cursor: pointer;
      box-sizing: border-box;
      text-align: center;
      border: 0;

      &[disabled] {
        color: #c0c4cc;
        cursor: not-allowed;
      }

      &.active {
        cursor: not-allowed;
        background-color: #409eff;
        color: #fff;
      }
    }
  }
</style>